﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Text;

namespace zsSearchAdminPages
{
    public partial class SearchAdminPages : System.Web.UI.UserControl
    {
        /// <summary>
        /// This list contains the central administration pages on which the usercontrol will be loaded.
        /// </summary>
        private IList<string> _centralAdminPages = new List<string>()
        {
            "default.aspx", //central admin homepage
            "applications.aspx",
            "systemsettings.aspx",
            "monitoring.aspx",
            "backups.aspx",
            "security.aspx",
            "upgradeandmigration.aspx",
            "generalapplicationsettings.aspx",
            "configurationwizards.aspx"
        };

        protected void Page_Load(object sender, EventArgs e)
        {
            Page page = HttpContext.Current.Handler as Page;
            string title = this.Page.Header.Title;
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            string fileName = url.Substring(url.LastIndexOf('/') + 1).ToLower();

            //check whether we are on a central admin page or a site settings page
            if (_centralAdminPages.Contains(fileName))
            {
                StringBuilder strbuild = new StringBuilder();
                strbuild.AppendLine("<script type=\"text/javascript\">");
                strbuild.AppendLine("$(document).ready(function () {");
                strbuild.AppendLine("if(g_wsaSiteTemplateId == 'CENTRALADMIN#0'){");
                strbuild.AppendLine("$('table#ctl00_PlaceHolderMain_ControlPanel tbody tr').quicksearch({");
                strbuild.AppendLine("position: 'before',");
                strbuild.AppendLine("attached: 'table#ctl00_PlaceHolderMain_ControlPanel',");
                strbuild.AppendLine("stripeRowClass: ['odd', 'even'],");
                strbuild.AppendLine("labelText: '',");
                strbuild.AppendLine("inputText: 'Search this page...',");
                strbuild.AppendLine("focusOnLoad: false });");
                strbuild.AppendLine("$('ul#list_example li').quicksearch({");
                strbuild.AppendLine("position: 'before',");
                strbuild.AppendLine("attached: 'ul#list_example',");
                strbuild.AppendLine("loaderText: '',");
                strbuild.AppendLine("delay: 1000,});};");
                strbuild.AppendLine("});");
                strbuild.AppendLine("</script>");
                
                this.Controls.Add(new LiteralControl(strbuild.ToString()));
            }
            else if (fileName == "settings.aspx")
            {
                StringBuilder strbuild = new StringBuilder();
                strbuild.AppendLine("<script type=\"text/javascript\">");
                strbuild.AppendLine("$(document).ready(function () {");
                strbuild.AppendLine("$('table#ctl00_PlaceHolderMain_ctl01_SettingLinksV4 tbody tr').quicksearch({");
                strbuild.AppendLine("position: 'before',");
                strbuild.AppendLine("attached: 'table#ctl00_PlaceHolderMain_ctl01_SettingLinksV4',");
                strbuild.AppendLine("stripeRowClass: ['odd', 'even'],");
                strbuild.AppendLine("labelText: '',");
                strbuild.AppendLine("inputText: 'Search this page...',");
                strbuild.AppendLine("focusOnLoad: false });");
                strbuild.AppendLine("$('ul#list_example li').quicksearch({");
                strbuild.AppendLine("position: 'before',");
                strbuild.AppendLine("attached: 'ul#list_example',");
                strbuild.AppendLine("loaderText: '',");
                strbuild.AppendLine("delay: 1000,});});");
                strbuild.AppendLine("</script>");

                this.Controls.Add(new LiteralControl(strbuild.ToString()));
            }
        }
    }
}
